<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\admin\model\Store as Model;
use app\admin\model\StoreMenu;
/**
 * 商户列表
 */
class Store extends AdminBase
{
    /**
     * 用户列表
     */
    public function index($limit='10')
    {
        $model = new Model();
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $model = $model->where('name|title|mobile','like','%'.$param['keyword'].'%');
        }
        $lists = $model->order('id desc')->paginate($limit);
        $this->result(compact('lists'));
    }

    /**
     * 修改用户
     */
    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = Model::savedata($param);
            $this->result('',$result['code'],$result['msg']);
        }
    }

    /**
     * 删除用户
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            Model::destroy($param['id']);
            $this->result('',0,"删除成功");
        }
    }

    /**
     * 重置密码
     */
    public function reset_password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            AdminModel::update($param,['id'=>$param['id']]);
            $this->result('',0,"重置成功");
        }
    }


    /**
     * 读取菜单列表
     */
    public function menu_list($store_id='')
    {
        //读取所有菜单
        $list = StoreMenu::where('status',1)->select();
        //读取当前权限的菜单
        if(!empty($store_id)){
            $role = Model::where('id',$store_id)->find();
        }else{
            $role['roles'] = [];
        }

        foreach ($list as $k=>$r){
            //如果存在
            $list[$k]['checked'] = in_array($r['id'],$role['roles']);
        }
        $this->result($list);
    }




}